﻿package stuff3D.sprites
{
	import editeur.events.MoveEvent;
	import fab.geom.Line;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	public class MouseDot extends Sprite
	{
		public var meshIndex:uint;
		public var vc3D:Vector3D;
		public var lineConstraint:Line;
		private var selected:Boolean;
		static public var selection:Vector.<MouseDot> = new Vector.<MouseDot>();
		
		public function MouseDot( mIndex:uint, vc3D:Vector3D )
		{
			this.meshIndex = mIndex;
			this.vc3D = vc3D;
			deselect()
			graphics.beginFill( 0xFF0000, .5 );
			graphics.drawCircle( 0, 0, 4 );
			graphics.endFill();
			
			addDragListeners();
		}

		public function addDragListeners():void
		{
			buttonMode = true;
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			addEventListener(MouseEvent.MOUSE_OVER, _survole );
		}
		public function _survole(event:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OVER, _survole );
			addEventListener(MouseEvent.MOUSE_OUT, _sort );
		}
		private function _sort(event:MouseEvent):void
		{
			addEventListener(MouseEvent.MOUSE_OVER, _survole );
			removeEventListener(MouseEvent.MOUSE_OUT, _sort );
		}		
		public function mouseDown(event:MouseEvent=null):void
		{
			switch_select();
			//start_dragging();
		}

		private function switch_select():void
		{
			if ( selected ) deselect(); else select();
		}
		public function deselect():void
		{
			switch_off();
			if ( selection.indexOf( this ) > 0 ) selection.splice( selection.indexOf( this ), 1 );
		}
		private function switch_off():void
		{
			selected = false;
			alpha = 0.2;
		}
		public function select():void
		{
			selected = true;
			alpha = 1;
			selection.push( this );
		}
		static public function removeFromSelection(mDot:MouseDot, index:int, vector:Vector.<MouseDot>):void
		{
			mDot.switch_off();
		}		
		static public function kill_selection():void
		{
			selection.forEach( removeFromSelection );
			selection = new Vector.<MouseDot>();			
		}
		
		public function neighbours(d:Number=2):void
		{
			
			
			
		}		
		
		public function start_dragging():void
		{
			//select();
			
			if ( lineConstraint == null ) return;
			
			//var vc3Dproj:Vector3D = new Vector3D();
			//vc3Dproj.x = vc3D.x;
			//vc3Dproj.y = 100;
			//vc3Dproj.z = vc3D.z;

			//trace(lineConstraint);
			
			//startDrag(  );//lockCenter
			stage.addEventListener(MouseEvent.MOUSE_MOVE, bouge);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
		}
		public function mouseReleased(event:MouseEvent):void
		{

			stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, bouge);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseReleased);
		}
		public function bouge(event:MouseEvent):void
		{
			var mouse:Point = new Point( event.stageX, event.stageY );
			var rel_mouse:Point = parent.globalToLocal( mouse );
			dispatchEvent( new MoveEvent( MoveEvent.MOVING, parent.globalToLocal(mouse) ) );
			//trace(lineConstraint);
			//send_new_params( ["x","y"] );
			
			
			x = rel_mouse.x;
			y = lineConstraint.getY( x );
			
			
			var abs:Number = lineConstraint.where( new Point( x, y ) );
			
			trace(abs);
			
		}			
	}
}